/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.fetcher.crawler;

import com.torunski.crawler.Crawler;
import com.torunski.crawler.events.ILoadingEventListener;
import com.torunski.crawler.events.IParserEventListener;
import com.torunski.crawler.model.MaxIterationsModel;
import java.util.prefs.Preferences;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.openide.util.NbPreferences;
import pl.tom.social.dal.entity.Site;
import pl.tom.social.fetcher.common.FetcherConsts;
import pl.tom.social.fetcher.gui.FetcherSettingsPanel;
import pl.tom.social.common.jade.TaskBarProxy;

/**
 *
 * @author Tom
 */
public class CrawlerFacade {

	private static final Logger logger = Logger.getLogger(CrawlerFacade.class);
	private static int maxIter;

	static {
		Preferences pref = NbPreferences.forModule(FetcherSettingsPanel.class);
		maxIter = pref.getInt("crawlerMaxIter", FetcherConsts.DEFAULT_MAX_ITERATIONS);
	}

	public static void processNode(Site site, TaskBarProxy statusBar) {
		logger.log(Level.DEBUG, "Setting up crawler for site " + site.getUrl());

		Crawler crawler = new Crawler();
		crawler.setLinkFilter(SocialLinkFilter.getFilter(site));
		crawler.setModel(new MaxIterationsModel(maxIter));
		crawler.setParser(new SocialHttpClientParser());

		IParserEventListener pListener = new SiteParserEventListener(site);
		crawler.addParserListener(pListener);
		ILoadingEventListener lListener = new UpdateTaskBarListener(statusBar);
		crawler.addLoadingListener(lListener);

		logger.log(Level.DEBUG, "Crawler for site " + site.getUrl() + " starting");
		crawler.start(site.getUrl(), "");
	}
}
